<template>
  <div>
    <!-- 旧票签收 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhOldInvoiceAccept"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :disabled="!isCan" :click="dataHandle">签收</multiuse-button>
        <multiuse-button size="small" type="primary" :disabled="!isCan" :click="dataReject">驳回</multiuse-button>

      </template>
    </FilterTable>

    <BaseDialog :visible.sync="isDealer" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
       
    <BaseDialog
      :visible.sync="rejectView"
      title="驳回原因"
      width="30%"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <div style="height:120px">
        <FormPanel
          ref="from2"
          :list="initForm2"
          :cols="3"
          :labelWidth="120"
          :isSubmitBtn="false"
          :collapse="false"
        />
      </div>
      <div
        :style="{
          textAlign: 'center'
        }"
      >
      <el-button  @click="clickReason" type="primary">确 定</el-button>
      <el-button  @click="rejectView = false">取 消</el-button>

    </div>
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, receiptInvoice , rejectInvoice} from '@/api/omFinance/InvoiceMailManage/OldInvoiceAccept';
import OwnerSearch from '../OwnerSearch';


export default {
  name: 'OldInvoiceAccept',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      isDealer: false,
      invoiceIds: [],
      isCan: false,
      rejectReason:'',
      rejectView:false,
      initForm2: this.initFormFun2(),
    };
  },
  components: {
    OwnerSearch
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
       initFormFun2() {
      return[
        {
          type: 'TEXT_AREA',
          label: '驳回原因',
          fieldName: 'xtspnr',
          placeholder: '请输入',
          initialValue: '',
          selfCols:3
        }
      ]
    },
    returnReason(value){

    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    selectChangeHandle(rows) {
      this.invoiceIds = [];
      if (rows.length > 0) {
        this.isCan = true;
        rows.map(x => {
          this.invoiceIds.push(x.invoiceId);
        });
        return;
      }
      this.isCan = false;
    },

    async dataHandle() {
      await this.$confirm(`是否签收`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = await receiptInvoice({ invoiceIds: this.invoiceIds });

          if (res.resultCode == 200) {
            this.$notify.success({ title: '提示信息', message: `签收成功！` });

            this.fetchParams = { ...this.fetchParams };
          }
          
        })
        .catch(() => {});
    },
    async clickReason(){
       this.$refs.from2.SUBMIT_FORM();
        var flag = false;
        this.initForm2.forEach(x => {
          if (x.fieldName === 'xtspnr' ) {
         
           this.rejectReason = x.initialValue

            if (x.initialValue === null || x.initialValue === undefined || x.initialValue === '') {
              flag = true
            }
          }
        })
        if (flag){
          this.$notify.error({ title: '提示信息', message: '请输入驳回原因！' || '操作失败!' });
          return false;
        }
        console.log(this.invoiceIds,this.rejectReason);

       let res = await rejectInvoice({ invoiceIds: this.invoiceIds, reason:this.rejectReason });
        if (res.resultCode === 200) {
            this.rejectView = false;
           
           this.$notify.success({ title: '提示信息', message: `驳回成功！` });

            this.fetchParams = { ...this.fetchParams };
        } else {
          this.$notify.error({title: '提示信息',message:res.errMsg||'驳回失败!'});
        }
    },
   async dataReject() {
      this.rejectView = true;
      this.rejectReason = "";
    this.initForm2.find(x=>x.fieldName=='xtspnr').initialValue = ''

      return;
      await this.$confirm(`是否驳回`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        inputPlaceholder:'请输入驳回原因',
      })
        .then(async () => {
      
      this.rejectView = true;
      this.rejectReason = "";
          // let res = await rejectInvoice({ invoiceIds: this.invoiceIds, reason:this.rejectReason });

          // if (res.resultCode == 200) {
          //   this.$notify.success({ title: '提示信息', message: `驳回成功！` });

          //   this.fetchParams = { ...this.fetchParams };
          // }

        })
        .catch(() => {});
    },
    seacherFun() {
      this.isDealer = true;
    },
    emitCloseHandler(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCodes').initialValue = dealerCodes.toString();
      }
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCodes',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '退回快递单号',
          fieldName: 'backCourierNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'RANGE_DATE',
          label: '退回快递日期',
          fieldName: 'backCourierStrartDate|backCourierEndDate',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '发票号',
          fieldName: 'goldenInvoiceNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'buzinessType',
          placeholder: '请选择',
          itemList: this.createDictList('6089')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '业务类型',
          dataIndex: 'buzinessType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6089'),
          render: props => {
            return <span>{this.createDictText(props.row.buzinessType, '6089')}</span>;
          }
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '发票号码',
          dataIndex: 'goldenInvoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票代码',
          dataIndex: 'goldenInvoiceCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '邮寄状态',
          dataIndex: 'postType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6090'),
          render: props => {
            return <span>{this.createDictText(props.row.postType, '6090')}</span>;
          }
        },
        {
          title: '店端签收状态',
          dataIndex: 'receiptStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6091'),
          render: props => {
            return <span>{this.createDictText(props.row.receiptStatus, '6091')}</span>;
          }
        },
        {
          title: '异常原因',
          dataIndex: 'abnormalReason',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原快递公司',
          dataIndex: 'courierCompany',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原快递单号',
          dataIndex: 'courierNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原快递日期',
          dataIndex: 'courierDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '店端签收日期',
          dataIndex: 'receiptDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '退回快递公司',
          dataIndex: 'backCourierCompany',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退回快递单号',
          dataIndex: 'backCourierNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退回快递日期',
          dataIndex: 'backCourierDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '备注',
          dataIndex: 'backRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
